package com.blb.controller;


import com.blb.entity.*;
import com.blb.service.IClassificationService;
import org.aspectj.weaver.IClassFileProvider;
import org.joda.time.DateTime;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;
import java.text.SimpleDateFormat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanghao
 * @since 2019-12-21
 */
@RestController
@RequestMapping("classification")
public class ClassificationController {
    @Resource
    private IClassificationService classificationService;

    @PostMapping("getclassifications")
    public DtreePage getMenu(Model model,String token){
        System.out.println(token);
        DtreePage dtreePage =new DtreePage();
        List<Classification> allClassification = classificationService.getAllClassification("0","0",token);
        List<Classification> list = classificationService.list();
        dtreePage.setData(allClassification);
//        dtreePage.setData(list);
        Map<String,String> map = new HashMap<String,String>();
        map.put("code","200");
        map.put("message","成功");
        dtreePage.setStatus(map);
        return dtreePage;
    }

    @PostMapping("addmenuroot")
    public ResponseHelp addmenuroot(Classification classification){
        String userId = classification.getUserId();
        String id = classification.getId();
        String pid = classification.getPid();
        Classification classification1 =new Classification();
        classification1.setId(UUID.randomUUID().toString().replace("-",""));
        classification1.setUserId(userId);
        classification1.setPid(id);
        classification.setIconUrl("img/folder1.png");
        classification1.setRecycle("0");
        Date date = new Date();
        String dealTime= new SimpleDateFormat("yyyy-MM-dd ").format(date);
        classification1.setCreateTime(dealTime);
        classification1.setMenuname("新建文件夹1");
        classificationService.save(classification1);
        System.out.println(classification);
        return ResponseHelp.success();
    }

    /**
     * 文件夹删除进入回收站
     * @param classification
     * @return
     */
    @PostMapping("updateRecycleById")
    public ResponseHelp updateRecycleById(Classification classification){
        String userId = classification.getUserId();
        String id = classification.getId();
        List<Classification> allClassification = classificationService.getAllClassification(id, "0", userId);
        Textttt textttt = new Textttt();
        List<String> ids = textttt.tttt(allClassification);
        System.out.println(ids);
        if(ids.size() == 0){
            classificationService.updateRecycleById("1",id);
            return ResponseHelp.success();
        }
        for (String id1:ids){
            classificationService.updateRecycleById("1",id1);
        }
        classificationService.updateRecycleById("1",id);
        return ResponseHelp.success();

    }

    /**
     * 查找一层文件夹
     * @param id
     * @return
     */
    @PostMapping("getallfolder")
    public List<Classification> getallfolder(String id,String token){
        List<Classification> allFolder;
        if(id==null){
            allFolder = classificationService.getAllFolder("0",token,"0");
        }else {
            allFolder = classificationService.getAllFolder(id,token,"0");
        }
        return allFolder;
    }

    /**
     * 查找一层笔记
     *
     * @param id
     * @return
     */
    @PostMapping("getallnotes")
    public List<Notes> getallnotes(String id,String token){
        System.out.println(id);
        List<Notes> allNotes;
        if(id==null){
            allNotes = classificationService.getAllNotes(token,"0");
        }else {
            allNotes = classificationService.getAllNotesByClassId(id,token,"0");
        }
        System.out.println(allNotes);
        return allNotes;
    }

    /**
     * 后退刷新文件夹
     * @param backId
     * @return
     */
    @PostMapping("backallfolder")
    public List<Classification> backallfolder(String backId,String token){
        Classification byId = classificationService.getById(backId);
        List<Classification> allFolder = classificationService.getAllFolder(byId.getPid(),token,"0");
        return allFolder;
    }

    /**
     * 后退刷新笔记
     * @param backId
     * @return
     */
    @PostMapping("backallnotes")
    public List<Notes> backallnotes(String backId,String token){
        System.out.println(backId);
        List<Notes> allNotes = classificationService.getAllNotesByClassId(backId,token,"0");
        return allNotes;
    }

    /**
     * 创建文件夹
     * @param
     * @return
     */
    @PostMapping("createfolder")
    public String createfolder(Classification classification){
        System.out.println(classification);
        classification.setId(UUID.randomUUID().toString().replace("-",""));
        classification.setCreateTime(new DateTime().toString("yyyy-MM-dd"));
        classification.setIconUrl("img/folder1.png");
        classification.setRecycle("0");
        classificationService.save(classification);
        return "success";
    }

    /**
     * 重命名文件夹
     */
    @PostMapping("renameFolder")
    public String renameFolder(@RequestParam("folderId") String folderId,@RequestParam("folderName") String folderName){
        Classification byId = classificationService.getById(folderId);
        byId.setMenuname(folderName);
        classificationService.saveOrUpdate(byId);
        return "success";
    }

    /**
     * 移动文件夹
     */
    @PostMapping("moveFolder")
    public String moveFolder(String folderId,String toId){
            Classification byId = classificationService.getById(folderId);
            byId.setPid(toId);
            classificationService.saveOrUpdate(byId);
        return "success";
    }


}

